package com.sicheng.lc.杂题.滑动窗口专题;

import java.util.HashMap;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/8/3 10:57
 */
public class 水果成篮 {
    //https://leetcode.cn/problems/fruit-into-baskets/
    static int k = 2;

    public int totalFruit(int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<>(16);
        int res = 0;
        for (int r = 0, l = 0; r < nums.length; r++) {
            map.put(nums[r], map.getOrDefault(nums[r], 0) + 1);
            while (l <= r && map.size() > k) {
                int x = map.get(nums[l]);
                map.put(nums[l], x - 1);
                if (x == 1)
                    map.remove(nums[l]);
                l++;
            }

            res = Math.max(res, r - l + 1);

        }
        return res;
    }
}
